<template>
  <div
    style="margin-top: 30px; margin-bottom: 30px"
    class="contact-us-info-container"
    @mouseenter="isHover = true"
    @mouseleave="isHover = false"
  >
  <!-- PC -->
    <div class="contact_us_wrap" :style="{ width: modelValue?.width + 'px' }">
      <div class="contact_us_img">
        <img :src="modelValue?.image"  :alt="modelValue?.imageAlt"></img>
      </div>
      <div class="contact_us_content">
        <h4 class="contact_us_title">{{modelValue?.title}}</h4>
        <div class="contact_us_content_wrap">
          <!-- 地址 -->
          <div style="display: flex;">
            <div style="margin-right:24px;" class="contact_us_content_item" v-if="pin.text">  
              <span
                class="icon_link_icon"
                :style="{ '--icon-url': `url(${pinIcon})` }"
              ></span>
              <span @click="routeClick(pin?.url,pin)"  :class="[pin.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{pin?.text}}</span>
            </div>
            <!-- 谷歌、百度 -->
             <div style="display: flex;">
              <div class="contact_us_content_item" v-if="googleMap.text">  
                <span
                  class="icon_link_icon"
                  :style="{ '--icon-url': `url(${googleIcon})` }"
                ></span>
                <span @click="routeClick(googleMap?.url,googleMap)"  :class="[googleMap.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{ googleMap?.text}}</span>
              </div>
              <div class="contact_us_content_item last_content" v-if="baiduMap.text">  
                <span
                  class="icon_link_icon"
                  :style="{ '--icon-url': `url(${baiduIcon})` }"
                ></span>
                <span @click="routeClick(baiduMap?.url,baiduMap)"  :class="[baiduMap.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{baiduMap?.text}}</span>
              </div>
             </div>
          </div>  
          <!-- 联系方式 -->
          <div style="display: flex;">
            <div class="contact_us_content_item"  v-if="phone.text">  
              <span
                class="icon_link_icon"
                :style="{ '--icon-url': `url(${phoneIcon})` }"
              ></span>
              <span @click="routeClick(phone?.url,phone)"  :class="[phone.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{phone?.text}}</span>
            </div>
            <div class="contact_us_content_item last_content" v-if="fax.text">  
              <span
                class="icon_link_icon"
                :style="{ '--icon-url': `url(${faxIcon})` }"
              ></span>
              <span @click="routeClick(fax?.url,fax)"  :class="[fax.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{fax?.text}}</span>
            </div>
          </div>
          <!-- 邮件 -->
          <div class="contact_us_content_item" v-if="email.text">
            <span
              class="icon_link_icon"
              :style="{ '--icon-url': `url(${emailIcon})` }"
            ></span>
            <span @click="routeClick(email?.url,email)"  :class="[email.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{email?.text}}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 移动端 -->
     <div class="contact_us_wrap_mobile">
      <div class="contact_us_img">
        <img :src="modelValue?.image"  :alt="modelValue?.imageAlt"></img>
      </div>
      <div class="content_mobile_detail">
       <h4>{{modelValue?.title}}</h4>
       <!-- 地址 -->
        <div style="margin-right:24px;" class="contact_us_content_item" v-if="pin.text">  
          <span
            class="icon_link_icon"
            :style="{ '--icon-url': `url(${pinIcon})` }"
          ></span>
          <span @click="routeClick(pin?.url,pin)"  :class="[pin.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{pin?.text}}</span>
        </div>
        <div class="contact_us_content_item" v-if="googleMap.text">  
          <span
            class="icon_link_icon"
            :style="{ '--icon-url': `url(${googleIcon})` }"
          ></span>
          <span @click="routeClick(googleMap?.url,googleMap)" :class="[googleMap.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{ googleMap?.text}}</span>
        </div>
        <div class="contact_us_content_item" v-if="baiduMap.text">  
          <span
            class="icon_link_icon"
            :style="{ '--icon-url': `url(${baiduIcon})` }"
          ></span>
          <span @click="routeClick(baiduMap?.url,baiduMap)" :class="[baiduMap.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{baiduMap?.text}}</span>
        </div>
        <div class="contact_us_content_item"  v-if="phone.text">  
          <span
            class="icon_link_icon"
            :style="{ '--icon-url': `url(${phoneIcon})` }"
          ></span>
          <span @click="routeClick(phone?.url,phone)" :class="[phone.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{phone?.text}}</span>
        </div>
        <div class="contact_us_content_item" v-if="fax.text">  
          <span
            class="icon_link_icon"
            :style="{ '--icon-url': `url(${faxIcon})` }"
          ></span>
          <span @click="routeClick(fax?.url,fax)" :class="[fax.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{fax?.text}}</span>
        </div>
        <div class="contact_us_content_item" v-if="email.text">
          <span
            class="icon_link_icon"
            :style="{ '--icon-url': `url(${emailIcon})` }"
          ></span>
          <span @click="routeClick(email?.url,email)" :class="[email.linkType === 'textOnly'?'item_text':'contact_us_content_item_text']">{{email?.text}}</span>
        </div>
      </div>
    </div>
    <Operate
      v-if="!isPreview"
      v-model:show="isHover"
      @handle-edit="showSetting = true"
      @handle-delete="handleDelete"
      @handle-copy="handleCopy"
      @handle-paste="handlePaste"
    ></Operate>
  </div>
  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref ,watch} from "vue";
import { randomString } from "@/utils/common";
import Setting from "./setting.vue";
// import Operate from "@/components/Operate/index.vue";
import Operate from '@/common/Operate/index.vue';
import baiduIcon from "@/assets/icon-baidu-map.svg"
import googleIcon from "@/assets/icon-google-map.svg"
import phoneIcon from "@/assets/icon-phone.svg"
import faxIcon from "@/assets/icon-fax.svg"
import emailIcon from "@/assets/icon-email.svg"
import pinIcon from "@/assets/icon-pin.svg"
// 定义组件名称
defineOptions({
  name: "ContactUsInfo",
});
const pin = ref({
  text:"",
  linkType:"",
  url:"",
})
const googleMap = ref({
  text:"",
  linkType:"",
  url:"",
})
const baiduMap = ref({
  text:"",
  linkType:"",
  url:"",
})
const phone = ref({
  text:"",
  linkType:"",
  url:"",
})
const fax = ref({
  text:"",
  linkType:"",
  url:"",
})
const email = ref({
  text:"",
  linkType:"",
  url:"",
})  

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {},
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true,
  },
});
console.log("!!!!", props.modelValue);
const emit = defineEmits(["update:modelValue", "handleDelete", "handleCopy", "handlePaste", "handleClick"]);

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit("handleDelete", props.modelValue.id);
};

const handleCopy = () => {
  emit("handleCopy", props.modelValue);
};

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

const routeClick = (url: string,item: { type: string; text: string; url: string ; linkType: string }) => {
  if(item.linkType === 'textOnly'){
    return
  }
  if(url.includes("http")){
    if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
    window.open(url, '_blank');
  } }
  }else{
    emit('handleClick', url)
  }
}

const handleInit = () => {
  if (props.modelValue && props.modelValue.image) return; // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: "ContactUsInfo",
    title: "title",
    width: 1208,
    image:"https://osswebsite-test.cedim.cn/media-library/ywies-tx/images/campus-life/school-facilities/02_public%20area%2001_origin_origin.jpg",
    imageAlt:"",
    linkList:[
    
    ],
   
  };
  emit("update:modelValue", data);
};

// 监听 modelValue 的变化
watch(
  () => props.modelValue,
  (newValue: any) => {
    console.log('modelValue 发生变化:', {
      新值: newValue,
      list:newValue?.linkList,
    });
    newValue?.linkList?.forEach((item: { type: string; text: string; url: string ; linkType: string }) => {
      if(item.type === "pin"){
       pin.value.text = item.text;
       pin.value.linkType = item.linkType;
       pin.value.url = item.url;
       console.log("pin===========",pin)
       console.log("item==========",item)
      }
      if(item.type === "googleMap"){
        googleMap.value.text = item.text;
        googleMap.value.linkType = item.linkType;
        googleMap.value.url = item.url;
      }
      if(item.type === "baiduMap"){
        baiduMap.value.text = item.text;
        baiduMap.value.linkType = item.linkType;
        baiduMap.value.url = item.url;
      }
      if(item.type === "phone"){
        phone.value.text = item.text;
        phone.value.linkType = item.linkType;
        phone.value.url = item.url;
      }
      if(item.type === "fax"){
        fax.value.text = item.text;
        fax.value.linkType = item.linkType;
        fax.value.url = item.url;
      }
      if(item.type === "email"){
        email.value.text = item.text;
        email.value.linkType = item.linkType;
        email.value.url = item.url;
      }
    })
    // 在这里添加你需要执行的逻辑
  },
  {
    deep: true, // 深度监听对象内部属性的变化
    immediate: true // 立即执行一次回调
  }
);
handleInit();
</script>

<style lang="scss" scoped>
.content_mobile_detail{
  text-align: left;
  h4{
    margin-top: 24px;
    margin-bottom: 16px;
  }
}
.last_content{
  margin-left:24px;
  padding-left:24px!important;
  border-left: 1px solid #e4e4e6!important;
  height:24px;
}
.contact_us_content_item_text{
  color: var(--text-color--primary, #0032a0);
  margin-left:8px;
}
.item_text{
    color: var(--text-color--primary, #0032a0);
  margin-left:8px;
  // 默认鼠标样式
  cursor:default ;
}
.icon_link_icon{
  display: block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-position: center;
  background-image: var(--icon-url); // 添加这一行
}
.contact-us-info-container {
  text-align: center;
  display: block;
  position: relative;
}
.contact_us_wrap {
  --max-width: 1920px;
  padding-left: 36px;
  padding-right: 36px;
  margin: 0 auto;
  max-width: calc(var(--max-width) + 72px);
  display: flex;
  align-items: center;
  
  // 添加媒体查询：只在电脑屏幕显示，手机屏幕隐藏
  @media (max-width: 768px) {
    display: none;
  }
}

// 添加移动端样式
.contact_us_wrap_mobile {
  display: none; // 默认隐藏
  
  // 只在手机屏幕分辨率下显示
  @media (max-width: 768px) {
    display: block;
    // 在这里可以添加移动端特定的样式
  }
}
.contact_us_img{
  width: 100%;
  max-width: 420px;
  border-radius: 4px;
  overflow: hidden;
  img{
    width: 100%;
    height: 100%;
  }
}
.contact_us_content{
  margin-left: 40px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  .contact_us_title{
    color: var(--text-color--primary, #0032a0);
    margin-bottom: 24px;
  }
}
.contact_us_content_item{
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  border: none;
  background: none;
  color: #0032a0;
  color: var(--text-color--primary, #0032a0);
  text-decoration: none;
  display: flex;
  align-items: center;
  padding: 0;
  cursor: pointer;
}
.contact_us_content_item:hover .contact_us_content_item_text {
  background-size: 100% 1px, 0 1px;
}

 .contact_us_content_item_text{
  text-decoration: none;
  background-image: linear-gradient(90deg, #0032a0, #0032a0);
  background-size: 0 1px, 100% 1px;
  background-position: 0 100%, 100% 100%;
  background-repeat: no-repeat;
  transition: background-size 0.3s;
  padding-bottom: 2px;
}
</style>
